package com.cmc.cloud.cmclink.doc.controller.admin.si;

import java.util.Collection;
import java.util.List;import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.cmc.cloud.cmclink.doc.api.document.emums.CostTypeEnum;import com.cmc.cloud.cmclink.doc.util.CostChargeUtil;
import com.cmc.cloud.cmclink.doc.util.CostChargeWithBlnoUtil;import com.cmc.cloud.cmclink.doc.vo.blnochargevo.BlnoLocalChargeResultVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cmc.cloud.cmclink.doc.api.document.emums.ExImTypeEnum;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.service.si.BlnoLocalCostImService;
import com.cmc.cloud.cmclink.doc.vo.blnofreightvo.BlnoFreightCalReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnolocalcostvo.BlnoLocalCostImAllRespVO;
import com.cmc.cloud.cmclink.doc.vo.blnolocalcostvo.BlnoLocalCostImCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnolocalcostvo.BlnoLocalCostImRespVO;
import com.cmc.cloud.cmclink.doc.vo.blnolocalcostvo.BlnoLocalCostImUpdateReqVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 进口提单本地费用前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 进口提单本地费用")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/blnoLocalCost/import")
public class BlnoLocalCostImController {

    @Resource
    private BlnoLocalCostImService blnoLocalCostImService;

    @Resource
    private CostChargeWithBlnoUtil costChargeWithBlnoUtil;

    @Operation(summary = "获得所有提单列表")
    @PostMapping("/list/bill")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:calculate-pod-agent-local-cost:query')")
    public CommonResult<List<BlnoLocalCostImRespVO>>
        getBlnoLocalCostImList(@Valid @RequestBody BlnoFreightCalReqVO reqVO) {
        List<BlnoLocalCostImRespVO> list = blnoLocalCostImService.getBlnoWithLocalCostList(reqVO);
        return CommonResult.success(list);
    }

    @Operation(summary = "计算运费")
    @GetMapping("/cal")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('doc:calculate-pod-agent-local-cost:cal')")
    public CommonResult<List<BlnoLocalChargeResultVO>> calLocalCostIm(@RequestParam("ids") Collection<Long> ids,
        @RequestParam("vendorCsmCode") String vendorCsmCode, @RequestParam("currentPort") String currentPort) {
        Map<Long, String> errMsgMap = blnoLocalCostImService.calLocalCostIm(ids, vendorCsmCode, ExImTypeEnum.IM.getValue(), currentPort);
        List<BlnoLocalChargeResultVO> rspList =
            costChargeWithBlnoUtil.transitMapToList(ids, errMsgMap, CostTypeEnum.LOCAL_COST_CAL_IM.getValue());
        return CommonResult.success(rspList);
    }

    @Operation(summary = "获得进口Local-Cost详情列表")
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @UseResponsePropertyAdvice
    public CommonResult<BlnoLocalCostImAllRespVO> getBlnoAllFreight(@PathVariable("id") Long id) {
        BlnoLocalCostImAllRespVO freightResp = blnoLocalCostImService.getBlnoAllLocalCost(id, ExImTypeEnum.IM.getValue());
        return CommonResult.success(freightResp);
    }

    @Operation(summary = "发送财务")
    @GetMapping("/account")
    @PreAuthorize("@ss.hasPermission('doc:calculate-pod-agent-local-cost:account')")
    public CommonResult<List<BlnoLocalChargeResultVO>> toAccountFreight(@RequestParam("ids") Collection<Long> ids) {
        Map<Long, String> errMsgMap = blnoLocalCostImService.toAccountLocalCost(ids, ExImTypeEnum.IM.getValue());
        List<BlnoLocalChargeResultVO> rspList =
            costChargeWithBlnoUtil.transitMapToList(ids, errMsgMap, CostTypeEnum.LOCAL_COST_ACCOUNT_IM.getValue());
        return CommonResult.success(rspList);
    }

    @Operation(summary = "创建进口Local-Cost费用")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:calculate-pod-agent-local-cost:create ')")
    public CommonResult<Void> createBlnoLocalCost(@Valid @RequestBody BlnoLocalCostImCreateReqVO createReqVO) {
        blnoLocalCostImService.createBlnoLocalCost(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "从本地成本明细复制")
    @PostMapping("/copy")
    @PreAuthorize("@ss.hasPermission('doc:calculate-pod-agent-local-cost:copy')")
    public CommonResult<Void> copyFromHistory(@Valid @RequestBody BlnoLocalCostImCreateReqVO createReqVO) {
        blnoLocalCostImService.createBlnoLocalCost(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新本地成本明细")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:calculate-pod-agent-local-cost:update')")
    public CommonResult<Void> updateBlnoLocalCost(@Valid @RequestBody BlnoLocalCostImUpdateReqVO updateReqVO) {
        blnoLocalCostImService.updateBlnoLocalCost(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除提单本地成本明细")
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:calculate-pod-agent-local-cost:delete')")
    public CommonResult<Void> deleteBlnoLocalCost(@PathVariable("id") Long id) {
        blnoLocalCostImService.deleteBlnoLocalCost(id);
        return CommonResult.success();
    }

    @Operation(summary = "从Local-charge复制")
    @PostMapping("/copy/income/{blnoId}")
    @PreAuthorize("@ss.hasPermission('doc:calculate-pod-agent-local-cost:copyImport')")
    public CommonResult<Void> copyFromIncome(@PathVariable("blnoId") Long blnoId) {
        blnoLocalCostImService.copyFromIncome(blnoId);
        return CommonResult.success();
    }
}
